package students.StudentInfo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class DeleteStudentFrame extends JFrame{
    JPanel buttonPanel = new JPanel();
    JPanel inputPanel = new JPanel(new GridBagLayout());

    //删除按钮
    JButton jb1 = new JButton("删除");
    //取消按钮
    JButton jb2 = new JButton("取消");

    //学号输入框
    JLabel jl1 = new JLabel("学号：");
    JTextField jtf1 = new JTextField();

    public DeleteStudentFrame() {
        setTitle("删除学生信息");
        setSize(300, 200);
        setVisible(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        //学号输入框
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.anchor = GridBagConstraints.CENTER;

        // 学号标签
        jl1.setPreferredSize(new Dimension(80, 20));
        gbc.gridx = 0;
        gbc.gridy = 0;
        inputPanel.add(jl1, gbc);

        // 输入框
        gbc.gridx = 1;
        jtf1.setPreferredSize(new Dimension(120, 25));
        inputPanel.add(jtf1, gbc);

        // 将输入面板居中
        this.add(inputPanel, BorderLayout.CENTER);

        //添加删除按钮事件
        jb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //获取输入的学号
                String sno = jtf1.getText();
                //判断学号是否为空
                if(sno.isEmpty()) {
                    JOptionPane.showMessageDialog(DeleteStudentFrame.this, "请输入学号", "提示", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                //删除学生信息
                try {
                    //创建连接
                    Connection conn = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/project001?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC",
                        "root",
                        "123456"
                    );
                    //创建SQL语句
                    String sql = "delete from student where sno = ?";
                    //创建PreparedStatement对象
                    PreparedStatement pstmt = conn.prepareStatement(sql);
                    //设置参数
                    pstmt.setInt(1, Integer.parseInt(sno));
                    //执行SQL语句
                    try {
                        int affectedRows = pstmt.executeUpdate();
                        if(affectedRows > 0) {
                            JOptionPane.showMessageDialog(
                                DeleteStudentFrame.this, 
                                "成功删除" + affectedRows + "条记录",
                                "操作结果",
                                JOptionPane.INFORMATION_MESSAGE
                            );
                        } else {
                            JOptionPane.showMessageDialog(DeleteStudentFrame.this, "未删除任何记录", "提示", JOptionPane.WARNING_MESSAGE);
                        }   
                    } catch (SQLException ex) {
                        JOptionPane.showMessageDialog(DeleteStudentFrame.this, "数据库异常：" + ex.getMessage(), 
                            "严重错误", JOptionPane.ERROR_MESSAGE);
                        ex.printStackTrace();
                    }
                    //关闭连接
                    pstmt.close();
                    conn.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        //添加取消按钮事件
        jb2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //关闭窗口
                dispose();
            }
        });
        //设置按钮大小
        jb1.setPreferredSize(new Dimension(100, 30));
        jb2.setPreferredSize(new Dimension(100, 30));
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
        buttonPanel.add(Box.createHorizontalGlue());
        buttonPanel.add(jb1);
        buttonPanel.add(Box.createRigidArea(new Dimension(20,0)));
        buttonPanel.add(jb2);
        buttonPanel.add(Box.createHorizontalGlue());
        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    }
}
